package com.moco.launcher.wifi;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnFocusChangeListener;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.moco.launcher.R;

public class WifiConnectFragment extends Fragment implements OnClickListener{
	String TAG = "WifiConnectFragment";
	View rootView;
	ConnectImpl connectImpl;
	//AccessPoint accessPoint;
	TextView wifi_connect_net_name,wifi_connect_signal_txt,wifi_connect_safe_txt,wifi_connect_txt;
	ImageView wifi_connect_exit;
	EditText wifi_connect_edit_passwd;
	
	/*private WifiManager mWifiManager;
	private WifiInfo mCurentLastInfo;
	private String mCurrentSSID;
	
	boolean isNeedForget = false;*/
	int showType = ConnectImpl.SHOWTYPE_NORMAL;
	

	
	public WifiConnectFragment(ConnectImpl connectImpl,AccessPoint accessPoint) {
		// TODO Auto-generated constructor stub
		this.connectImpl = connectImpl;
//		this.accessPoint = accessPoint;
	}
	
	@Override
	public void onCreate(Bundle savedInstanceState) { 
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		/*mWifiManager = (WifiManager) getActivity().getSystemService(Context.WIFI_SERVICE);
		mCurentLastInfo = mWifiManager.getConnectionInfo();
		mCurrentSSID = mCurentLastInfo.getSSID();
		mCurrentSSID = mCurrentSSID.substring(1, mCurrentSSID.length()-1);
		Log.i(TAG, "mCurrentSSID is:"+mCurrentSSID);*/
		Log.i(TAG, "enter oncreate");
	}
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		Log.i(TAG, "enter onCreateView");
		

		
		rootView = inflater.inflate(R.layout.wifi_connect_layout, container,false);
		wifi_connect_net_name = (TextView)rootView.findViewById(R.id.wifi_connect_net_name);
		wifi_connect_signal_txt = (TextView)rootView.findViewById(R.id.wifi_connect_signal_txt);
		wifi_connect_safe_txt = (TextView)rootView.findViewById(R.id.wifi_connect_safe_txt);
		wifi_connect_txt = (TextView)rootView.findViewById(R.id.wifi_connect_txt);
		
		
		wifi_connect_exit = (ImageView)rootView.findViewById(R.id.wifi_connect_exit);
		wifi_connect_edit_passwd = (EditText)rootView.findViewById(R.id.wifi_connect_edit_passwd);
		
		//wifi_connect_edit_passwd.setInputType(InputType.TYPE_TEXT_VARIATION_PASSWORD); 
		//wifi_connect_edit_passwd.setOnFocusChangeListener(new EditOnFocusChangeListener());
		wifi_connect_edit_passwd.setOnClickListener(this);
		
		initListener();
		//return super.onCreateView(inflater, container, savedInstanceState);
		return rootView;
	}
	
	public void showInputPwd(String pwd){
		wifi_connect_edit_passwd.setText(pwd);
	}
	
	private void initListener(){
		wifi_connect_exit.setOnClickListener(this);
		wifi_connect_txt.setOnClickListener(this);
	}
	
	class EditOnFocusChangeListener implements OnFocusChangeListener{
		@Override
		public void onFocusChange(View v, boolean hasFocus) {
			// TODO Auto-generated method stub
			//Toast.makeText(getActivity(), "enter focus", Toast.LENGTH_LONG).show();
			Log.i(TAG, "enter EditOnFocusChangeListener");
			/*if(hasFocus && !isNeedForget)
				connectImpl.showInputKeyboard();*/
		}
	}
	
	@Override
	public void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		Bundle bundle = getArguments();
		if(bundle==null)
			return;
		initDataForUI(bundle);
	}
	
	private void initDataForUI(Bundle bundle){
		String ssid = bundle.getString("wifi_name");
		showType = bundle.getInt("show_type");
		Log.i(TAG, "enter initDataForUI showType is:"+showType);
		wifi_connect_edit_passwd.setText("");
		
		switch (showType) {
		case ConnectImpl.SHOWTYPE_NORMAL:
			wifi_connect_txt.setText(getActivity().getResources().getString(R.string.wifi_net_item_connect));
			wifi_connect_edit_passwd.setVisibility(View.VISIBLE);
			break;
		case ConnectImpl.SHOWTYPE_OLD:
			wifi_connect_edit_passwd.setVisibility(View.VISIBLE);
			wifi_connect_edit_passwd.setText("********");
			break;
		case ConnectImpl.SHOWTYPE_CONNECTED:
			wifi_connect_txt.setText(getActivity().getResources().getString(R.string.wifi_forget_connect));
			wifi_connect_edit_passwd.setVisibility(View.VISIBLE);
			wifi_connect_edit_passwd.setText("********");
			break;
		default:
			break;
		}
		int wifi_level = bundle.getInt("wifi_signal");
		Log.i(TAG, "wifi_level is:"+wifi_level);
		String security = bundle.getString("wifi_security");
		//if(accessPoint != null){
			wifi_connect_net_name.setText(ssid);
			String level = null;
			if (wifi_level>2){
				level = "强";
			}else if(wifi_level>=1&&wifi_level<=2){
				level = "一般";
			}
			else{
				level = "弱";
			}
			
			Log.i(TAG, "levle=="+level);
			wifi_connect_signal_txt.setText("信号强度："+level);
			wifi_connect_safe_txt.setText("安全性："+security);
		//}
	}
	
	@Override
	public void onSaveInstanceState(Bundle outState) {
		// TODO Auto-generated method stub
		super.onSaveInstanceState(outState);
		Log.i(TAG, "enter wifiConnectFragement onSaveInstanceState");
	}
	
	@Override
	public void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
	}
	
	@Override
	public void onDestroyView() {
		// TODO Auto-generated method stub
		super.onDestroyView();
	}
	
	@Override
	public void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
	}
	

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		if(v==wifi_connect_txt){
			String pwdText =wifi_connect_edit_passwd.getText().toString();
			if(pwdText.equals("")||pwdText.length()<8){
				Toast.makeText(getActivity(), "密码输入有误", Toast.LENGTH_SHORT).show();
				return;
			}
			switch(showType){
			case ConnectImpl.SHOWTYPE_NORMAL:
				
				
				wifi_connect_edit_passwd.setText("");
				connectImpl.exit(ConnectImpl.CONNECT);
				break;
			case ConnectImpl.SHOWTYPE_OLD:
				//Log.e("test wifi ples del", "case ConnectImpl.SHOWTYPE_OLD:");
				//??
			/*	if(wifi_connect_edit_passwd.getText().equals("********")){
					connectImpl.exit(ConnectImpl.CONNECTBYFIG);
				}else{
					connectImpl.exit(ConnectImpl.CONNECT);
				}*/
				connectImpl.exit(ConnectImpl.CONNECTBYFIG);
				break;
			case ConnectImpl.SHOWTYPE_CONNECTED:
				Log.i(TAG, "forget connected");
				connectImpl.exit(ConnectImpl.FORGET);
				break;
			default:
				break;
			}
		}else if(v== wifi_connect_exit){
			getActivity().onBackPressed();
		}else if(v == wifi_connect_edit_passwd){
			Log.i(TAG, "enter onClick showType is:"+showType);
			switch(showType){
			case ConnectImpl.SHOWTYPE_CONNECTED:
				//connectImpl.showInputKeyboard();
				break;
			case ConnectImpl.SHOWTYPE_OLD:
				wifi_connect_edit_passwd.setText("");
				connectImpl.exit(ConnectImpl.FORGET_NOEXIT);
				connectImpl.showInputKeyboard();
				break;
			case ConnectImpl.SHOWTYPE_NORMAL:
				connectImpl.showInputKeyboard();
				break;
			default:
				break;
			}
		}
	}

}
